﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Threading;
using System.Windows.Threading;
using DeployGames.TweenEngine.TestBed.core;

namespace DeployGames.TweenEngine.TestBed.wpf
{
    public class Program
    {
        private static bool eternalLoop = true;

        [STAThread]
        public static void Main(string[] args)
        {
            Application app = new Application();
            MainWindow win = new MainWindow();

            app.Exit += new ExitEventHandler(app_Exit);

            Clock clk = new Clock();
            FPSCounter counter = new FPSCounter();
            win.Show();
            clk.Reset();
            counter.Frequency = clk.Frequency;
            counter.RefreshRate = clk.Frequency / 2;

            double delta, time = 0f;
            long tick;

            Thread thread = new Thread(new ParameterizedThreadStart((a) =>
            {
                while (eternalLoop)
                {
                    tick = clk.TicksDelta();
                    delta = (double)tick / clk.Frequency;
                    time += delta;
                    counter.tick(tick);
                    Application.Current.Dispatcher.BeginInvoke(
                        DispatcherPriority.Normal,
                        new Action(() =>
                        {
                            if (win != null)
                            {
                                win.Update(delta, time);
                                win.Title = counter.ToString();
                                win.UpdateLayout();
                            }
                        }));
                    Thread.Sleep(16);
                }
            }));
            thread.Start();
            app.Run(win);
        }

        static void app_Exit(object sender, ExitEventArgs e)
        {
            eternalLoop = false;
        }
    }
}
